/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment4;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author chrisjaramillo
 */
public class OutputNode {
    double m_input;
    double m_weight;
    double m_output;
    double m_threshold;
    NeuralNetwork m_network;
    
    OutputNode()
    {
        init();
        m_network = null;
    }
    
    OutputNode(NeuralNetwork network)
    {
        init();
        m_network = network;
    }
    
    private void init()
    {
        m_input = 0.0;
        m_weight = 0.0;
        m_output = 0.0;
        m_threshold = 0.0;
    }
    
    public void setRandomWeights()
    {
        System.out.println("Output node");
        int hiddenNodeCount = m_network.hiddenNodes().size();
        Random random = new Random();
        double val = random.nextDouble();
        double range = (2.4/hiddenNodeCount)*2.0;
        val *= range;
        val -= range/2.0;
        m_weight = val;
        System.out.println("Weight: " + val);
        val = random.nextDouble();
        val *= range;
        val -= range/2.0;
        m_threshold = val;
        
    }
    
    public void value(int nodeNumber)
    {
        ArrayList hiddenNodes = m_network.hiddenNodes();
        double value = 0.0;
        for(int i=0; i<hiddenNodes.size(); i++)
        {
            HiddenNode node = (HiddenNode)hiddenNodes.get(i);
            double hiddenNodeVal = node.value();
            double weight = node.weight(nodeNumber);
            value += hiddenNodeVal * weight - m_threshold;
        }
        m_input = value;
        m_output = 1/(1+Math.pow(Math.E,(-1 * value)));
    }
    
    public double value()
    {
        return m_output;
    }
    
    public double weight()
    {
        return m_weight;
    }
}
